package com.homework.service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.base.day8.JDBCComon;
import com.base.util.KeyUtil;
import com.homework.entity.Homewokmaster;
import com.homework.entity.Homeworkdetail;
import com.homework.entity.SubmitVo;

public class HomeWorkService {
	JDBCComon jdbc=new JDBCComon();
	Connection conn=null;
	/**
	 * 根据open date 查询作业。 目前用于今日竞赛
	 * @return
	 */
	public ArrayList<Homewokmaster> getHomewokmasterByDateAndType(long sreachDate,String type){
		ArrayList<Homewokmaster> ls=new ArrayList<Homewokmaster>();
		try {
			conn=jdbc.getConnection();
			String sql="select * from homeworkmaster where open_date=? and type=?";
			PreparedStatement ps=conn.prepareStatement(sql);
			Date sdate=new Date(sreachDate);
			System.out.println("sdate=="+sdate);
			System.out.println("type=="+type);
			ps.setDate(1, sdate);
			ps.setString(2, type);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				Homewokmaster hk=new Homewokmaster();
				hk.setId(rs.getString("id"));
				System.out.println("id=="+hk.getId());
				hk.setOpen_date(rs.getDate("open_date"));
				hk.setTitle(rs.getString("title"));
//				String t1=rs.getString("type");
//				hk.setType(t1);
				hk.setType(rs.getString("type"));
				hk.setUpdate_time(rs.getDate("update_time"));
				hk.setContent(rs.getString("content"));
				ls.add(hk);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ls;
		
	}
	
	
	
	
	/**
	 * 根据主表id 查询作业。 目前用于今日竞赛
	 * @return
	 */
	public ArrayList<Homeworkdetail> getHomeworkdetailByMasterId(String mid){
		ArrayList<Homeworkdetail> ls=new ArrayList<Homeworkdetail>();
		try {
			conn=jdbc.getConnection();
			String sql="select * from homeworkdetail where master_id=?";
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, mid);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				Homeworkdetail hk=new Homeworkdetail();
				hk.setId(rs.getString("id"));
				hk.setContent(rs.getString("content"));
				hk.setMaster_id(rs.getString("master_id"));
				hk.setUpdate_time(rs.getDate("update_time"));
				ls.add(hk);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ls;
		
	}
	
	
	/**
	 * 根据主表id 查询提交情况
	 * @return
	 */
	public ArrayList<SubmitVo> getHomeworkdetailByMasterId2(String mid){
		ArrayList<SubmitVo> ls=new ArrayList<SubmitVo>();
		try {
			conn=jdbc.getConnection();
			String sql="select u.name,uh.homework_id from user u left join user_homework uh on (u.id=uh.user_id and uh.homework_id=?)";
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, mid);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				SubmitVo hk=new SubmitVo();
				String hid=rs.getString("homework_id");
				if(hid==null||"".equals(hid)){
					hk.setIsSubmit("未提交");
				}else{
					hk.setIsSubmit("已提交");
				}
				
				hk.setUserName(rs.getString("name"));
				ls.add(hk);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ls;
		
	}
	
	
	public boolean addHomeWork(Homewokmaster hmaster){
		
		String sql="INSERT INTO homeworkmaster (id, title, `type`, open_date, content) VALUES(?, ?, ?, ?, ?);";
		boolean flag=false;
		try {
			conn=jdbc.getConnection();
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, KeyUtil.genUniqueKey());
			ps.setString(2,hmaster.getTitle() );
			ps.setString(3,hmaster.getType());
			ps.setDate(4,hmaster.getOpen_date());
			ps.setString(5,hmaster.getContent() );
			if(ps.executeLargeUpdate()>0){
				flag=true;
			};
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}
	public boolean submitHomeWork(String uid, String hid){
		
		String sql="INSERT INTO user_homework (user_id, homework_id) VALUES(?, ?);";
		boolean flag=false;
		try {
			conn=jdbc.getConnection();
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1, uid);
			ps.setString(2,hid );
			if(ps.executeLargeUpdate()>0){
				flag=true;
			};
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return flag;
	}

}
